package eu.news.servlet.query;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import eu.news.data.dao.DAOFactory;
import eu.news.data.dao.NewsDAO;
import eu.news.data.pojo.News;
import eu.news.index.NewsIndexDAO;
import eu.news.template.WebPage;

/**
 * Servlet implementation class Search
 */
public class Search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Search() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String keyword=request.getParameter("keyword");
		NewsDAO dao = DAOFactory.getNewsDAO();
		List<News> newses=new ArrayList<News>();
		for(int id:NewsIndexDAO.search(keyword)){
			News news=dao.getById(id);
			if(news!=null)
				newses.add(news);
		}
		WebPage page = new WebPage(request, response, "default", "searchResult");
		page.setParameter("keyword", keyword);
		page.setParameter("newses", newses);
		page.forward();
	}

}
